<?php
declare (strict_types = 1);

namespace app\middleware;

use tauthz\facade\Enforcer;
use think\Response;
use think\Request;


class AdminAuth
{

    /**
     * 处理请求
     *
     * @param Request $request
     * @param \Closure    $next
     * @throws
     * @return Response
     */
    public function handle(Request $request, \Closure $next):Response
    {

        $user = $request->route();

        //验证权限
        $controller_path = strtolower($request->controller());
        $action_path = strtolower($request->action());
        $controller_arr = explode('.',$controller_path);//解析路由分组
        if(count($controller_arr) > 1){
            $controller_path = array_pop($controller_arr);
        }
        if($user['auth_user_status'] < 99 && !Enforcer::enforce($user['auth_user_name'],$controller_path,$action_path)){
            $err=[
                'code' => 403,
                'msg'  => '没有相关权限！',
                'data' => []
            ];
            json($err)->send();exit;
        }

        return $next($request);
    }
}
